package com.tuanzi.loan.business.service.family.impl;

import com.alibaba.fastjson.JSON;
import com.tuanzi.loan.business.entity.family.ClassificationInfo;
import com.tuanzi.loan.business.entity.family.MemberRights;
import com.tuanzi.loan.business.repository.family.MemberRightsRepository;
import com.tuanzi.loan.business.repository.family.MemberRightsRepositoryCustom;
import com.tuanzi.loan.business.service.family.ClassificationService;
import com.tuanzi.loan.business.service.family.MemberRightsService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.MemberRightsRequest;
import com.tuanzi.loan.vo.response.family.MemberRightsResponse;
import com.tuanzi.loan.vo.view.family.MemberRightsStatus;
import com.tuanzi.loan.vo.view.family.MemberRightsView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class MemberRightsServiceImpl implements MemberRightsService {

    @Autowired
    private MemberRightsRepository jpaRpsoitory;

    @Autowired
    private MemberRightsRepositoryCustom repositoryCustom;

    @Autowired
    private ClassificationService service;

    @Override
    public Page<MemberRightsResponse> list(PageSearcher<MemberRightsRequest> pageSearcher) {
        return repositoryCustom.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public MemberRights saveOrUpdateRights(MemberRightsView rightsView) throws InvocationTargetException, IllegalAccessException {
        MemberRights memberRights = new MemberRights();
        BeanUtils.copyProperties(rightsView,memberRights);
        List<Integer> user_type = rightsView.getUser_type();
        List<Integer> platform_type = rightsView.getPlatform_type();
        MemberRightsStatus memberRightsStatus = new MemberRightsStatus();
        memberRightsStatus.setUser_type(user_type);
        memberRightsStatus.setPlatform_type(platform_type);
        String multiple_type = JSON.toJSONString(memberRightsStatus);
        memberRights.setMultiple_type(multiple_type);
        memberRights.setProduct_id(JSON.toJSONString(rightsView.getProduct_id()));
        if(memberRights.getId() != null){
            MemberRights memberRightsFromDB = jpaRpsoitory.findOne(memberRights.getId());
            memberRights.setCreate_time(memberRightsFromDB.getCreate_time());
            memberRights.setUpdate_time(new Date());
        }else{
            memberRights.setCreate_time(new Date());
        }
        ClassificationInfo classificationInfo = service.getJpaRepository().getOne(memberRights.getClassification_id());
        memberRights.setClassification_name(classificationInfo.getClassif_name());
        jpaRpsoitory.save(memberRights);
        return memberRights;
    }

    @Override
    public void deleteRights(Long id) {
        jpaRpsoitory.delete(id);
    }

    @Override
    public MemberRightsView getMemberRightsView(Long id) {
        MemberRights memberRights = jpaRpsoitory.getOne(id);
        MemberRightsView mView = new MemberRightsView();
        BeanUtils.copyProperties(memberRights,mView);
        String multiple_type = memberRights.getMultiple_type();
        MemberRightsStatus memberRightsStatus = JSON.parseObject(multiple_type, MemberRightsStatus.class);
        mView.setPlatform_type(memberRightsStatus.getPlatform_type());
        mView.setUser_type(memberRightsStatus.getUser_type());
        mView.setProduct_id(JSON.parseArray(memberRights.getProduct_id(),String.class));
        return mView;
    }
}
